#
# 构建 php-fpm 5.6.33
#

FROM e7sky/was:3.7
MAINTAINER joen kai168@126.com

#https://download.savannah.gnu.org/releases/freetype/freetype-2.9.tar.gz
#http://www.ijg.org/files/jpegsrc.v6b.tar.gz
#http://www.ijg.org/files/jpegsrc.v8d.tar.gz
#https://github.com/libgd/libgd/releases/download/gd-2.2.5/libgd-2.2.5.tar.gz
#https://curl.haxx.se/download/curl-7.58.0.tar.gz
#https://download.sourceforge.net/libpng/libpng-1.6.34.tar.gz
#ftp://ftp-osl.osuosl.org/pub/libpng/src/libpng16/libpng-1.6.34.tar.gz
#https://git.gnome.org/browse/libxml2/snapshot/libxml2-2.9.7.tar.xz
#https://github.com/libgd/libgd/releases/download/gd-2.2.5/libgd-2.2.5.tar.gz
#http://www.bzip.org/1.0.6/bzip2-1.0.6.tar.gz

ENV PHP_VERSION 5.6.33
ENV IMAGEMAGICK_VERSION 6.9.9-35
ENV IMAGICK_VERSION 3.4.3
ENV OPENSSL_VERSION 1_0_2j

#COPY software/ImageMagick-$IMAGEMAGICK_VERSION.zip software/imagick-$IMAGICK_VERSION.tgz  software/openssl-$OPENSSL_VERSION.tar.gz software/php-$PHP_VERSION.tar.gz /was/soft/

RUN apk add --no-cache --virtual .build-deps g++ make perl unzip autoconf wget \
			&& apk add --no-cache --virtual .run-deps libxml2-dev jpeg-dev libpng-dev gd bzip2-dev curl curl-dev freetype-dev zlib-dev pcre-dev \
			&& cd /was/soft && gosu was wget -P /was/soft https://github.com/openssl/openssl/archive/OpenSSL_$OPENSSL_VERSION.tar.gz \
			&& cd /was/soft && gosu was wget -O /was/soft/php-$PHP_VERSION.tar.gz http://hk2.php.net/get/php-$PHP_VERSION.tar.gz/from/this/mirror \
			# && cd /was/soft && gosu was curl -fSL -o php-$PHP_VERSION.tar.gz http://hk2.php.net/get/php-$PHP_VERSION.tar.gz/from/this/mirror \
			# && cd /was/soft && gosu was wget -O /was/soft/php-$PHP_VERSION.tar.gz https://github.com/php/php-src/archive/php-$PHP_VERSION.tar.gz \
			&& cd /was/soft && gosu was wget -O /was/soft/ImageMagick-$IMAGEMAGICK_VERSION.tar.gz https://github.com/ImageMagick/ImageMagick/archive/$IMAGEMAGICK_VERSION.tar.gz \
			&& cd /was/soft && gosu was wget -O /was/soft/imagick-$IMAGICK_VERSION.tar.gz https://github.com/mkoppanen/imagick/archive/$IMAGICK_VERSION.tar.gz \
			&& cd /was/soft && gosu was tar xzf OpenSSL_$OPENSSL_VERSION.tar.gz && cd openssl-OpenSSL_$OPENSSL_VERSION && gosu was ./config --prefix=/was/run/openssl && gosu was make -j$(getconf _NPROCESSORS_ONLN) && gosu was make install \
# RUN cd /was/soft && gosu was tar xzf zlib-$ZLIB_VERSION.tar.gz && cd zlib-$ZLIB_VERSION && gosu was ./configure --prefix=/was/run/zlib && gosu was make -j$(getconf _NPROCESSORS_ONLN) && gosu was make install && cd ..
# RUN cd /was/soft && gosu was tar xzf pcre-$PCRE_VERSION.tar.gz && cd pcre-$PCRE_VERSION && gosu was ./configure --prefix=/was/run/pcre && gosu was make -j$(getconf _NPROCESSORS_ONLN) && gosu was make install && cd .. 
# RUN cd /was/soft && gosu was tar xzf jpegsrc.v$LIBJPEG_VERSION.tar.gz && cd jpeg-$LIBJPEG_VERSION && gosu was ./configure --prefix=/was/run/libjpeg && gosu was make -j$(getconf _NPROCESSORS_ONLN) && gosu was make install && cd .. 
# RUN cd /was/soft && gosu was tar xzf libpng-$LIBPNG_VERSION.tar.gz && cd libpng-$LIBPNG_VERSION && CPPFLAGS='-I/was/run/zlib/include' export CPPFLAGS  && LDFLAGS="-L/was/run/zlib/lib" export LDFLAGS && LD_LIBRARY_PATH="/was/run/zlib/lib:$LD_LIBRARY_PATH" export LD_LIBRARY_PATH && gosu was ./configure --prefix=/was/run/libpng && gosu was make -j$(getconf _NPROCESSORS_ONLN) && gosu was make install && cd .. 
# RUN cd /was/soft && gosu was tar xzf bzip2-1.0.6.tar.gz && gosu was make install PREFIX=/was/run/bzip2 && cd ..
# RUN cd /was/soft && gosu was tar xzf freetype-2.9.tar.gz && ./configure --prefix=/was/run/freetype BZIP2_CFLAGS="-I/was/run/bzip2/include" BZIP2_LIBS="-L/was/run/bzip2/lib" ZLIB_CFLAGS="-I/was/run/zlib/include" ZLIB_LIBS="-L/was/run/zlib/lib" LIBPNG_CFLAGS="-I/was/run/libpng/include" LIBPNG_LIBS="-L/was/run/libpng/lib" && make -j$(getconf _NPROCESSORS_ONLN) && gosu was make install && cd ..  
# RUN cd /was/soft && gosu was tar xzf libgd-2.2.5.tar.gz
# RUN cd /was/soft && gosu was tar xf libxml2-2.9.7.tar.xz 
# RUN cd /was/soft && gosu was tar xzf curl-7.58.0.tar.gz
#       && apk add  --no-cache  --virtual .run-deps \
#       	libxml2 libxml2-dev \
#       	libjpeg libjpeg-dev \
#       	libpng libpng-dev \
#       	gd \
#       	bzip2 bzip2-dev \
#       	curl curl-dev \
#       	freetype freetype-dev \
#       && cd /was/soft cd/openssl-1.0.2j \
#       && gosu was ./config --prefix=/was/run/openssl \
#       && gosu was make install \
#       && cd /was/soft/zlib-1.2.11 \
#       && gosu was ./configure --prefix=/was/run/zlib \
#       && gosu was make install \
#       && cd /was/soft/pcre-8.41 \
#       && gosu was ./configure --prefix=/was/run/pcre \
#       && gosu was make install \
# RUN cd /was/soft 
			&& cd /was/soft \
			&& gosu was tar xzf php-$PHP_VERSION.tar.gz \
      && cd php-$PHP_VERSION \
      && gosu was ./configure --prefix=/was/run/fpm \
      	--with-config-file-path=/was/run/fpm/etc \
      	--with-fpm-user=was \
      	--with-fpm-group=g_was \
      	--with-openssl=/was/run/openssl \
      	--with-zlib \
      	--enable-fpm \
      	--enable-zip \
      	--enable-bcmath \
      	--enable-sockets \
      	--enable-mbstring \
      	--with-gd \
      	--enable-gd-native-ttf \
      	--with-freetype-dir \
      	--with-jpeg-dir \
      	--with-mysql \
      	--with-curl \
      	--with-pdo-mysql \
      	--with-mysqli \
      	--without-pear \
      	--disable-fileinfo \
      && gosu was make -j$(getconf _NPROCESSORS_ONLN) \
      && gosu was make install \
      && gosu was cp php.ini-production /was/run/fpm/etc/php.ini \
      && gosu was sh -c "echo \"date.timezone = PRC\" >> /was/run/fpm/etc/php.ini" \
      && gosu was cp /was/run/fpm/etc/php-fpm.conf.default /was/run/fpm/etc/php-fpm.conf \
      && gosu was sh -c "echo '[global]' >> /was/run/fpm/etc/php-fpm.conf" \
      && gosu was sh -c "echo 'daemonize = no' >> /was/run/fpm/etc/php-fpm.conf" \
      && gosu was sh -c "echo 'error_log = /was/run/fpm/logs/php-fpm.log' >> /was/run/fpm/etc/php-fpm.conf" \
      && gosu was sh -c "echo '[www]' >> /was/run/fpm/etc/php-fpm.conf" \
      && gosu was sh -c "echo 'listen = 9000' >> /was/run/fpm/etc/php-fpm.conf" \
      && gosu was sh -c "echo 'catch_workers_output = yes' >> /was/run/fpm/etc/php-fpm.conf" \
      && gosu was mkdir -p /was/run/fpm/logs \
			# 安装 imagemagick 支持
			&& cd /was/soft \
      && gosu was tar xzf ImageMagick-$IMAGEMAGICK_VERSION.tar.gz \
      && gosu was tar xzf imagick-$IMAGICK_VERSION.tar.gz \
      && cd /was/soft/ImageMagick-$IMAGEMAGICK_VERSION && gosu was ./configure --prefix=/was/run/imagick && gosu was make -j$(getconf _NPROCESSORS_ONLN) && gosu was make install \
      && cd /was/soft/imagick-$IMAGICK_VERSION && gosu was /was/run/fpm/bin/phpize . && gosu was ./configure --with-php-config=/was/run/fpm/bin/php-config --with-imagick=/was/run/imagick/ && gosu was make -j$(getconf _NPROCESSORS_ONLN) && gosu was make install \
      && gosu was sh -c "echo \"[imagick]\" >> /was/run/fpm/etc/php.ini" \
      && gosu was sh -c "echo 'extension=\"imagick.so\"' >> /was/run/fpm/etc/php.ini" \
			# 移除 apk
	 		&& rm -Rf /was/soft/* \
			&& apk del .build-deps \
			&& apk add --no-cache --virtual .imagick-deps libgomp

EXPOSE 9000
CMD ["/was/run/fpm/sbin/php-fpm"]


